package org.dromara.answer.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.answer.domain.vo.AiUserAnswersVo;
import org.dromara.answer.domain.bo.AiUserAnswersBo;
import org.dromara.answer.service.IAiUserAnswersService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 用户答题记录
 *
 * @author Lion Li
 * @date 2025-04-09
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/answer/userAnswers")
public class AiUserAnswersController extends BaseController {

    private final IAiUserAnswersService aiUserAnswersService;

    /**
     * 查询用户答题记录列表
     */
    @SaCheckPermission("answer:userAnswers:list")
    @GetMapping("/list")
    public TableDataInfo<AiUserAnswersVo> list(AiUserAnswersBo bo, PageQuery pageQuery) {
        return aiUserAnswersService.queryPageList(bo, pageQuery);
    }

    /**
     * 根据用户考试记录ID查询答题记录列表
     */
    @SaCheckPermission("answer:userAnswers:list")
    @GetMapping("/listByUserExamId")
    public TableDataInfo<AiUserAnswersVo> listByUserExamId(@RequestParam Long userExamId, PageQuery pageQuery) {
        AiUserAnswersBo bo = new AiUserAnswersBo();
        bo.setUserExamId(userExamId);
        return aiUserAnswersService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出用户答题记录列表
     */
    @SaCheckPermission("answer:userAnswers:export")
    @Log(title = "用户答题记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AiUserAnswersBo bo, HttpServletResponse response) {
        List<AiUserAnswersVo> list = aiUserAnswersService.queryList(bo);
        ExcelUtil.exportExcel(list, "用户答题记录", AiUserAnswersVo.class, response);
    }

    /**
     * 获取用户答题记录详细信息
     *
     * @param recordId 主键
     */
    @SaCheckPermission("answer:userAnswers:query")
    @GetMapping("/{recordId}")
    public R<AiUserAnswersVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long recordId) {
        return R.ok(aiUserAnswersService.queryById(recordId));
    }

    /**
     * 新增用户答题记录
     */
    @SaCheckPermission("answer:userAnswers:add")
    @Log(title = "用户答题记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AiUserAnswersBo bo) {
        return toAjax(aiUserAnswersService.insertByBo(bo));
    }

    /**
     * 修改用户答题记录
     */
    @SaCheckPermission("answer:userAnswers:edit")
    @Log(title = "用户答题记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AiUserAnswersBo bo) {
        return toAjax(aiUserAnswersService.updateByBo(bo));
    }

    /**
     * 删除用户答题记录
     *
     * @param recordIds 主键串
     */
    @SaCheckPermission("answer:userAnswers:remove")
    @Log(title = "用户答题记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{recordIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] recordIds) {
        return toAjax(aiUserAnswersService.deleteWithValidByIds(List.of(recordIds), true));
    }
}
